<script setup lang="ts">
import { http } from '@/utils/http'
import { useUserStore } from '@/store/user'
import {
  ACCESS_TOKEN,
  USER_NAME,
  USER_INFO,
  APP_ROUTE,
  APP_CONFIG,
  HOME_CONFIG_EXPIRED_TIME,
  HOME_PAGE,
} from '@/common/constants'

const systemInfo = uni.getSystemInfoSync()
const { statusBarHeight } = systemInfo
const navHeight = 44

const userStore = useUserStore()
onMounted(() => {
})

const handleWxLogin = async () => {
    try {
        // 1. 获取微信 code
        const loginRes = await uni.login({
          provider: 'weixin' // 指定微信登录
        });
        const code = loginRes.code;
        console.log(code, 'code')

        // 2. 发送 code 到服务器
        const phone = '19961890661'
        const res = await http.post('/movieSystem/xcx/getXcxUserInfo', {
            memberCode: phone
        })
        // 3. 登录成功，存储 token
        if (res.data.token) {
          userStore.setUserInfo({
            token: res.data.token,
            memberCode: phone
         })
          uni.setStorageSync('token', res.data.token);
          uni.showToast({ title: '登录成功' });
          // 跳转到首页
          uni.reLaunch({ url: '/pages/movieList' });
        }
      } catch (err) {
        uni.showToast({ title: '登录失败', icon: 'none' });
        console.error('登录错误:', err);
      }
}
</script>

<template>
<PageLayout :navbarShow="true" navLeftText="" :navLeftArrow="false">
 <view  :style="{ 'height':'80vh' }" class="flex justify-center items-center" >
    <wd-button @click="handleWxLogin">一键登录</wd-button>
  </view>
  </PageLayout>
</template>

<style></style>

<route type="home" lang="json5">
{
  "style": {
    navigationStyle: 'custom',
    navigationBarTitleText: '一键登录',
  }
}
</route>
